/*     */ package com.gillion.grule.client.utils;
/*     */ 
/*     */ import java.lang.reflect.Method;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.beanutils.BeanUtils;
/*     */
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TempletUtil
/*     */ {
/*  20 */   public static Map map = new HashMap<>();
/*     */   
/*  22 */   private static Map<String, Method> methodMap = new HashMap();
/*     */   public static Object executeTemplet(String className, String methodName, Object... params) throws Exception {
/*     */     Object object;
/*  25 */     Method method = methodMap.get(className + methodName);
/*  26 */     if (method == null) {
/*  27 */       method = getMethod(className, methodName);
/*     */     }
/*     */     
/*     */     try {
/*  31 */       object = ContextHolder.appContext.getBean(className);
/*  32 */     } catch (Exception e) {
/*  33 */       object = null;
/*     */     } 
/*  35 */     if (object == null) {
/*  36 */       object = Class.forName(className).newInstance();
/*     */     }
/*  38 */     Class<?>[] parameterTypes = method.getParameterTypes();
/*  39 */     if (params.length != parameterTypes.length) {
/*  40 */       throw new Exception("入参个数不匹配");
/*     */     }
/*  42 */     List<Object> param = new ArrayList();
/*  43 */     for (int i = 0; i < params.length; i++) {
/*  44 */       if (params[i] instanceof Map || params[i] instanceof List) {
/*  45 */         param.add(mapToObject(params[i], parameterTypes[i]));
/*     */       } else {
/*  47 */         param.add(objectToClass(params[i], parameterTypes[i]));
/*     */       } 
/*     */     } 
/*     */     
/*  51 */     Object resule = method.invoke(object, param.toArray());
/*  52 */     return resule;
/*     */   }
/*     */   
/*     */   private static Method getMethod(String className, String methodName) throws Exception {
/*     */     Object object;
/*     */     try {
/*  58 */       object = ContextHolder.appContext.getBean(className);
/*  59 */     } catch (Exception e) {
/*  60 */       object = null;
/*     */     } 
/*  62 */     if (object == null) {
/*  63 */       object = Class.forName(className).newInstance();
/*     */     }
/*  65 */     Method[] methods = object.getClass().getMethods();
/*  66 */     for (Method method : methods) {
/*  67 */       if (method.getName().equals(methodName)) {
/*  68 */         methodMap.put(className + methodName, method);
/*  69 */         return method;
/*     */       } 
/*     */     } 
/*  72 */     return null;
/*     */   }
/*     */   
/*     */   private static Object mapToObject(Object map, Class<?> clazz) {
/*  76 */     if (map == null) {
/*  77 */       return null;
/*     */     }
/*  79 */     Object obj = null;
/*     */     try {
/*  81 */       if (map.getClass() == clazz) {
/*  82 */         obj = map;
/*     */       } else {
/*  84 */         obj = clazz.newInstance();
/*  85 */         BeanUtils.copyProperties(obj, map);
/*     */       } 
/*  87 */     } catch (Exception e) {
/*  88 */       e.printStackTrace();
/*     */     } 
/*  90 */     return obj;
/*     */   }
/*     */   
/*     */   private static Object objectToClass(Object value, Class<?> clazz) {
/*  94 */     if (value == null) {
/*  95 */       return null;
/*     */     }
/*  97 */     Object obj = value;
/*  98 */     if (clazz.getName().equals("java.lang.Integer")) {
/*  99 */       obj = Integer.valueOf(value.toString());
/* 100 */     } else if (clazz.getName().equals("java.lang.Double")) {
/* 101 */       obj = Double.valueOf(value.toString());
/* 102 */     } else if (clazz.getName().equals("java.lang.Long")) {
/* 103 */       obj = Long.valueOf(value.toString());
/* 104 */     } else if (clazz.getName().equals("java.lang.Float")) {
/* 105 */       obj = Float.valueOf(value.toString());
/* 106 */     } else if (clazz.getName().equals("java.lang.String")) {
/* 107 */       obj = obj.toString();
/*     */     } 
/* 109 */     return obj;
/*     */   }
/*     */ }


/* Location:              D:\apache-tomcat-9.0.76\webapps\grule-manager\WEB-INF\lib\grule-client-2.0.2.RELEASE.jar!\com\gillion\grule\clien\\utils\TempletUtil.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */